<?php

namespace app\controllers\admin\uni\system;

use app\controllers\admin\base\AdminController;
use app\controllers\h5\mini\MiniQrcodeController;
use app\models\Red;
use app\models\SetUserUp as Model;

class UserController extends AdminController
{
    public function actionFetch()
    {
        $model = Model::getLastOne();

        return $this->success($model);
    }

    public function actionSave()
    {
        $data = $this->post();

        if (Model::saveData($data)) {
            return $this->success();
        }

        return $this->fail();
    }

    // 生成红包
    public function actionSaveRed()
    {
        // 设置脚本最大执行时间为30秒
        set_time_limit(0);

        // 设置脚本最大内存使用量为128MB
        ini_set('memory_limit', '12800M');

        $min = $this->post('min_amount');
        $max = $this->post('max_amount');
        $count = $this->post('count');

        if ($min <= 0 || $max <= 0) {
            return $this->fail('金额错误');
        }

        if ($count < 1) {
            return $this->fail('数量错误');
        }

        if ($count > 1000) {
            return $this->fail('每次最多只能生成1000个');
        }

        $min *= 100;
        $max *= 100;

        for ($i = 1; $i <= $count; $i++) {

            $red = new Red();
            $red->amount = mt_rand($min, $max) / 100;
            $red->save();

            MiniQrcodeController::actionRed($red);

        }

        return $this->success();
    }

}